
package com.scs.application.modules.upms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.upms.entity.AccessLog;
import com.scs.application.modules.upms.mapper.AccessLogMapper;
import com.scs.application.modules.upms.request.AccessLogQueryRequest;
import com.scs.application.modules.upms.service.AccessLogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * service 实现
 *
 */
@AllArgsConstructor
@Slf4j
@Service
public class AccessLogServiceImpl extends BaseServiceImpl<AccessLogMapper, AccessLog> implements AccessLogService {

//    private final ElasticsearchTemplate elasticsearchTemplate;

    @Override
    public boolean saveCascadeById(AccessLog entity) {
        return true;
//        IndexQuery indexQuery = new IndexQueryBuilder()
//                .withId(entity.getId())
//                .withObject(entity).build();
//
//        return elasticsearchTemplate.index(indexQuery) != null;
    }

    @Override
    public List<AccessLog> list(AccessLogQueryRequest request) {
//        List<AccessLog> list = this.list(Wrappers.<AccessLog>query()
//                .eq(StringUtils.isNotBlank(request.getUserCode()), "user_code", request.getUserCode())
//                .like(StringUtils.isNotBlank(request.getRequestUri()), "request_uri", request.getRequestUri())
//                .eq(StringUtils.isNotBlank(request.getRequestMethod()), "request_method", request.getRequestMethod())
//                .between(request.getBeginRequestTime() != null && request.getEndRequestTime() != null, "request_time", request.getBeginRequestTime(), request.getEndRequestTime())
//            .orderByDesc("gmt_modified")
//        );
//        return list;

//        SearchQuery query = new NativeSearchQueryBuilder()
//                .build();
//        return elasticsearchTemplate.queryForList(query, AccessLog.class);
        return  null;
    }

    @Override
    public IPage<AccessLog> page(AccessLogQueryRequest request) {
//        IPage page = this.page(request.getPage(), Wrappers.<AccessLog>query()
//                .eq(StringUtils.isNotBlank(request.getUserCode()), "user_code", request.getUserCode())
//                .like(StringUtils.isNotBlank(request.getRequestUri()), "request_uri", request.getRequestUri())
//                .eq(StringUtils.isNotBlank(request.getRequestMethod()), "request_method", request.getRequestMethod())
//                .between(request.getBeginRequestTime() != null && request.getEndRequestTime() != null, "request_time", request.getBeginRequestTime(), request.getEndRequestTime())
//                .orderByDesc("gmt_modified")
//        );
//        Page page = new Page(request.getCurrPage(), request.getPageSize());
//        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder()
//                .withPageable(PageRequest.of(request.getCurrPage() - 1, request.getPageSize()))
//                .withSort(SortBuilders.fieldSort("requestTime").order(SortOrder.DESC));
//        if (StringUtils.isNotBlank(request.getRequestUri())) {
//            builder.withQuery(QueryBuilders.matchQuery("requestUri", request.getRequestUri()));
//        }
//        if (StringUtils.isNotBlank(request.getUserCode())) {
//            builder.withQuery(QueryBuilders.termQuery("userCode.keyword", request.getUserCode()));
//        }
//        if (StringUtils.isNotBlank(request.getRequestMethod())) {
//            builder.withQuery(QueryBuilders.termQuery("requestMethod.keyword", request.getRequestMethod()));
//        }
//        if (StringUtils.isNotBlank(request.getIsException())) {
//            builder.withQuery(QueryBuilders.termQuery("isException.keyword", request.getIsException()));
//        }
//        if (StringUtils.isNotBlank(request.getExceptionType())) {
//            builder.withQuery(QueryBuilders.termQuery("exceptionType.keyword", request.getExceptionType()));
//        }
//        SearchQuery query = builder.build();
//        AggregatedPage<AccessLog> aggregatedPage = elasticsearchTemplate.queryForPage(query, currentModelClass());
//
//        page.setRecords(elasticsearchTemplate.queryForList(query, AccessLog.class));
//        page.setTotal(aggregatedPage.getTotalElements()).setPages(aggregatedPage.getTotalPages());
//        return page;
        return  null;
    }
}
